package edu.lehigh.cse.ale;

import org.andengine.audio.sound.Sound;
import org.andengine.opengl.texture.region.TiledTextureRegion;

import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;

/**
 * Destinations are objects that the hero must reach in order to complete a level
 */
public class Destination extends PhysicsSprite
{
    /*
     * BASIC FUNCTIONALITY
     */

    /**
     * number of _heroes who can fit at /this/ destination
     */
    int _capacity;

    /**
     * number of _heroes already in /this/ destination
     */
    int _holding;

    /**
     * number of type 1 goodies that must be collected before this destination accepts any heroes
     */
    int _activationScore1;

    /**
     * number of type 2 goodies that must be collected before this destination accepts any heroes
     */
    int _activationScore2;

    /**
     * number of type 3 goodies that must be collected before this destination accepts any heroes
     */
    int _activationScore3;

    /**
     * number of type 4 goodies that must be collected before this destination accepts any heroes
     */
    int _activationScore4;

    /**
     * Create a destination
     * 
     * This should never be called directly.
     * 
     * @param x
     *            X coordinate of top left corner of this destination
     * @param y
     *            X coordinate of top left corner of this destination
     * @param width
     *            Width of this destination
     * @param height
     *            Height of this destination
     * @param ttr
     *            Image to display
     * @param isStatic
     *            Can this destination move, or is it at a fixed location
     * @param isCircle
     *            true if this should use a circle underneath for its collision detection, and false if a box should be
     *            used
     */
    private Destination(float x, float y, float width, float height, TiledTextureRegion ttr)
    {
        super(x, y, width, height, ttr, SpriteId.DESTINATION);
        _capacity = 1;
        _holding = 0;
        _activationScore1 = 0;
        _activationScore2 = 0;
        _activationScore3 = 0;
        _activationScore4 = 0;
    }

    /**
     * Add a simple destination that uses a circle as its _fixture
     * 
     * @param x
     *            X coordinate of top left corner of this destination
     * @param y
     *            X coordinate of top left corner of this destination
     * @param width
     *            Width of this destination
     * @param height
     *            Height of this destination
     * @param imgName
     *            Name of the image to display
     * @return the Destination, so that it can be manipulated further
     */
    public static Destination makeAsCircle(float x, float y, float width, float height, String imgName)
    {
        // get the image, create a destination, and give it default _physics
        TiledTextureRegion ttr = Media.getImage(imgName);
        Destination dest = new Destination(x, y, width, height, ttr);
        dest.setCirclePhysics(0, 0, 0, BodyType.StaticBody, false);
        dest.setCollisionEffect(false);

        // add the destination to the scene
        Level._current.attachChild(dest._sprite);

        // return the destination, so it can be modified
        return dest;
    }

    /**
     * Add a simple destination that uses a box as its _fixture
     * 
     * @param x
     *            X coordinate of top left corner of this destination
     * @param y
     *            X coordinate of top left corner of this destination
     * @param width
     *            Width of this destination
     * @param height
     *            Height of this destination
     * @param imgName
     *            Name of the image to display
     * @return the Destination, so that it can be manipulated further
     */
    public static Destination makeAsBox(float x, float y, float width, float height, String imgName)
    {
        // get the image, create a destination, and give it default _physics
        TiledTextureRegion ttr = Media.getImage(imgName);
        Destination dest = new Destination(x, y, width, height, ttr);
        dest.setBoxPhysics(0, 0, 0, BodyType.StaticBody, false);
        dest.setCollisionEffect(false);

        // add the destination to the scene
        Level._current.attachChild(dest._sprite);

        // return the destination, so it can be modified
        return dest;
    }

    /**
     * Change the number of type-1 goodies that must be collected before the destination accepts any heroes (the default
     * is 0)
     * 
     * @param score
     *            The number of goodies that must be collected.
     * 
     * @deprecated Use setActiviationScore[1-4]() instead
     */
    @Deprecated
    public void setActivationScore(int score)
    {
        _activationScore1 = score;
    }

    /**
     * Change the number of type-1 goodies that must be collected before the destination accepts any heroes (the default
     * is 0)
     * 
     * @param score
     *            The number of goodies that must be collected.
     */
    public void setActivationScore1(int score)
    {
        _activationScore1 = score;
    }

    /**
     * Change the number of type-2 goodies that must be collected before the destination accepts any heroes (the default
     * is 0)
     * 
     * @param score
     *            The number of goodies that must be collected.
     */
    public void setActivationScore2(int score)
    {
        _activationScore2 = score;
    }

    /**
     * Change the number of type-3 goodies that must be collected before the destination accepts any heroes (the default
     * is 0)
     * 
     * @param score
     *            The number of goodies that must be collected.
     */
    public void setActivationScore3(int score)
    {
        _activationScore3 = score;
    }

    /**
     * Change the number of type-4 goodies that must be collected before the destination accepts any heroes (the default
     * is 0)
     * 
     * @param score
     *            The number of goodies that must be collected.
     */
    public void setActivationScore4(int score)
    {
        _activationScore4 = score;
    }

    /**
     * Change the number of _heroes that can be accepted by this destination (the default is 1)
     * 
     * @param _heroes
     *            The number of _heroes that can be accepted
     */
    public void setHeroCount(int heroes)
    {
        _capacity = heroes;
    }

    /*
     * COLLISION SUPPORT
     */

    /**
     * Destinations are the last collision detection entity, so their collision detection code does nothing.
     * 
     * @param other
     *            Other object involved in this collision
     */
    void onCollide(PhysicsSprite other)
    {
    }

    /*
     * AUDIO SUPPORT
     */

    /**
     * Sound to play when a hero arrives at this destination
     */
    Sound _arrivalSound;

    /**
     * Specify the sound to play when a hero arrives at this destination
     * 
     * @param sound
     *            The sound file name that should play
     */
    public void setArrivalSound(String soundName)
    {
        _arrivalSound = Media.getSound(soundName);
    }
}